//
//  ZPMineViewController.m
//  ZPIOSEducation
//
//  Created by mini1 on 2016/11/30.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPMineViewController.h"
#import "ZPMineData.h"
#import "ZPMyInfoHeaderTabView.h"
#import "ZPMyInfoFirstTabViewCell.h"
#import "ZPMyInfoSecondTabViewCell.h"
#import "ZPMyinfoCollectionViewCell.h"

#import "ZPMineSetViewController.h"
#import "MessageViewController.h"
#import "ZPMineScoreViewController.h"
#import "ZPRegisterLoginViewController.h"
#import "ZPBaseNavigationController.h"


#import "ZPMineInformationVC.h"
#import "ZPMineOrderViewController.h"
#import "ZPMineClassViewController.h"
#import "ZPMineQRCodeViewController.h"
#import "ZPMineChangeStuViewController.h"
#import "CourseListViewController.h"
#import "ZPMineHelpCenterViewController.h"
#import "ZPMineFeedbackViewController.h"

//#import "NTalkerChatViewController.h"
//#import "XNGoodsInfoModel.h"
//#import "XNSDKCore.h"

extern NSString  *existUnreadMsg;

static NSString * const reuseIdentifier_firstCell            = @"ZPMyInfoFirstTabViewCell";
static NSString * const reuseIdentifier_secondCell           = @"ZPMyInfoSecondTabViewCell";
static NSString * const reuseIdentifier_collectionViewCell   = @"ZPMyinfoCollectionViewCell";

@interface ZPMineViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>
{
    BOOL      viewIsShow;  // 视图是否显现
    UIView   *signView;    // 显示签到view
    int       score;       // 签到积分
    NSString *signStr;     // 是否签到变换文字
    UIColor  *signBgColor;
    UIColor  *signStrColor;
    
    NSString  *scoreStr;
}
@property (strong, nonatomic) ZPMineData                 *mineData;

@property (strong, nonatomic) UIButton                   *rightNavBtn;

@property (strong, nonatomic) UIButton                   *leftNavBtn;

@property (strong, nonatomic) UICollectionViewFlowLayout *myInfoFlowLayout;

@property (strong, nonatomic) UICollectionView           *myInfoCollectView;

@property (weak, nonatomic)   IBOutlet UITableView       *myInfoTableView;

@property (strong, nonatomic) ZPCallAlertView            *callAlertView;

@property (strong, nonatomic) ZPMyInfoHeaderTabView      *headerView;

@property (strong, nonatomic) ZPMyInfoFirstTabViewCell   *firstCell;

@property (nonatomic)         BOOL                        isLogin; // 标记是否登录
@property (copy, nonatomic)   NSString                   *userName;
@property (copy, nonatomic)   NSString                   *memberId;
@property (copy, nonatomic)   NSString                   *atoken;
@property (copy, nonatomic)   NSString                   *stuName;
@end


@implementation ZPMineViewController


- (ZPMyInfoHeaderTabView *)headerView {
    if (_headerView == nil) {
        _headerView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZPMyInfoHeaderTabView class]) owner:self options:nil] lastObject];
    }
    return _headerView;
}

- (ZPMyInfoFirstTabViewCell *)firstCell {
    if (_firstCell == nil) {
        _firstCell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZPMyInfoFirstTabViewCell class]) owner:self options:nil] lastObject];
    }
    return _firstCell;
}

- (UICollectionViewFlowLayout *)myInfoFlowLayout { //CollectionView布局
    if (_myInfoFlowLayout == nil) {
        _myInfoFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    }
    return _myInfoFlowLayout;
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        viewIsShow = NO;
    }
    return self;
}



#pragma mark - <数据刷新>
// 根据网络刷新数据
- (void)refreshData
{
    [self checkLogIn];           // 检测是否登录
    [self loadUserHeadImage];    // 加载用户头像
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:Z_IMAGE(@"zhenpin_set") style:UIBarButtonItemStylePlain target:self action:@selector(clickSetBtnItem)];
    [[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(refreshNavRightImage) name:@"changeMessageIconStatus" object:nil];
    scoreStr = @"";
    [self refreshData];
    signBgColor  = zMainColor;
    signStrColor = white_color;
    self.automaticallyAdjustsScrollViewInsets = false;
    self.myInfoTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.myInfoTableView setTableHeaderView:[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100, FLT_MIN)]];
    [self addEventMomberView];   // 手势点击姓名进入资料界面
    [self initData];             // 添加界面显示view数据
 // [self enterMemberCenter];    // 点击姓名进入资料界面
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    viewIsShow = YES;
    [self refreshNavRightImage];
    // 根据登录是否刷新界面
    [self checkLogIn];           // 检测是否登录
    [self loadUserHeadImage];    // 加载用户头像
    
}

- (void)refreshNavRightImage
{
    if ([existUnreadMsg  integerValue] == 1) {
        UIButton *b=[UIButton buttonWithType:UIButtonTypeCustom];
        [b setBackgroundImage:[UIImage imageNamed:@"Message_RedPoint"] forState:UIControlStateNormal];
        [b addTarget:self action:@selector(clickNewsBtnItem) forControlEvents:UIControlEventTouchUpInside];
        b.bounds = CGRectMake(0, 0, 22, 22);
        UIBarButtonItem *b2=[[UIBarButtonItem alloc]initWithCustomView:b];
        self.navigationItem.rightBarButtonItems = @[b2];
    }else {
        UIButton *b=[UIButton buttonWithType:UIButtonTypeCustom];
        [b setBackgroundImage:[UIImage imageNamed:@"zhenpin_un_news"] forState:UIControlStateNormal];
        [b addTarget:self action:@selector(clickNewsBtnItem) forControlEvents:UIControlEventTouchUpInside];
        b.bounds = CGRectMake(0, 0, 22, 22);
        UIBarButtonItem *b2=[[UIBarButtonItem alloc]initWithCustomView:b];
        self.navigationItem.rightBarButtonItems = @[b2];
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    viewIsShow = NO;
}



- (void)initData {
    self.mineData = [[ZPMineData alloc] init];
    self.myInfoTableView .delegate  = self;
    self.myInfoTableView.dataSource = self;
    
}



- (void)requestSignData
{
    if (self.isLogin) {
        [self  showHudithString:@"加载中..."];
        NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"memberId"] ,@"memberId", nil];
        [[ZPNetworking  sharedZPNetworking] POST:APIUC@"user/isSign.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
            
        } success:^(id  _Nonnull responseObject) {
            NSLog(@"========%@",responseObject);
            [self  hiddenHud];
            if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
                if([[[responseObject objectForKey:@"result"] objectForKey:@"isSignIn"] boolValue]){
                    signStr      = @"已签到";
                    signBgColor = clear_color;
                    signStrColor = UIColorFromRGB(0xFF5656);
                }else{
                    scoreStr = [[responseObject objectForKey:@"result"] objectForKey:@"score"];
                    signStr = [NSString  stringWithFormat:@"签到+%@",scoreStr];
                }
                [self.myInfoTableView reloadData];
            }
        } failure:^(NSError * _Nonnull error) {
            [self  hiddenHud];
            
        }];
    }
}

- (void)signAction
{
    if (self.isLogin) {
        [self  showHudithString:@"加载中..."];
        NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"memberId"] ,@"memberId",@"1",@"type", nil];
        [[ZPNetworking  sharedZPNetworking] POST:APIUC@"user/addScore.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
            
        } success:^(id  _Nonnull responseObject) {
            [self  hiddenHud];
            if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
                score = [[[responseObject objectForKey:@"result"] objectForKey:@"score"] intValue];
                
                NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserInfo.plist"];
                NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
                [dataDic  setObject:[NSString  stringWithFormat:@"%@",[[responseObject objectForKey:@"result"] objectForKey:@"score"]] forKey:@"score"];
                [dataDic  writeToFile:homePath atomically:YES];
                [[ZPAuthStore  sharedZPAuthStore] resetUserInfoDictionary];
                
                [self showSignView]; // 显示签到view
                [self performSelector:@selector(hiddenSignView) withObject:nil afterDelay:1.];
                signStr = @"已签到";
                [self.firstCell.myInfoSign setTitleColor:UIColorFromRGB(0xFF5656) forState:UIControlStateNormal];
//                signBgColor = clear_color;
//                signStrColor = UIColorFromRGB(0xFF5656);
                [self.myInfoTableView reloadData];
            }
            
        } failure:^(NSError * _Nonnull error) {
            [self  hiddenHud];
            
        }];
    }
}



#pragma mark - <点击进入设置界面>
- (void)clickSetBtnItem
{
    ZPMineSetViewController *mineSetVc = [[ZPMineSetViewController alloc] init];
    [self.navigationController pushViewController:mineSetVc animated:YES];
}



#pragma mark - <点击进入消息界面>
- (void)clickNewsBtnItem
{
    if (!self.isLogin) {// 判断是否登录
        [self login];
        return;
    }
    MessageViewController *vc = [[MessageViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - <进入我的资料>
- (void)enterMemberCenter
{
    if (!self.isLogin) {// 判断是否登录
        [self login];
        return;
    }
    ZPMineInformationVC *infoVc = [[ZPMineInformationVC alloc] init];
    infoVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:infoVc animated:YES];
}




#pragma mark - <点击登录>
- (void)login
{
        ZPRegisterLoginViewController *regLoginVc = [[ZPRegisterLoginViewController alloc] init];
        ZPBaseNavigationController *nvc = [[ZPBaseNavigationController alloc] initWithRootViewController:regLoginVc];
        [self presentViewController:nvc animated:YES completion:nil];
}



- (void)addEventMomberView{ // 点击姓名直接进入资料界面
    self.headerView.myInfoName.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterMemberCenter)];
    tap.numberOfTapsRequired = 1;
    [self.headerView.myInfoName addGestureRecognizer:tap];
}





#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];
    if (section == 0)
    {
        ZPMyInfoFirstTabViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_firstCell];
        if (cell == nil)
        {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ZPMyInfoFirstTabViewCell" owner:self options:nil] lastObject];
        }
        cell.hotlineBtn.hidden = YES;
        cell.myInfoScoreLab.text = [NSString stringWithFormat:@"我的积分  %d",score];
        
        if([signStr isEqualToString:@"已签到"]){
            signBgColor = clear_color;
            signStrColor = UIColorFromRGB(0xFF5656);
        }else{
            signBgColor = UIColorFromRGB(0x21A4EB);
            signStrColor = UIColorFromRGB(0xFFFFFF);
        }
        
        [cell.myInfoSign setTitle:signStr forState:UIControlStateNormal];
        cell.myInfoSign.backgroundColor = signBgColor;
        [cell.myInfoSign setTitleColor:signStrColor forState:UIControlStateNormal];
        [cell.myInfoScore addTarget:self action:@selector(myScoreBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [cell.myInfoSign  addTarget:self action:@selector(myInfoSignClick) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    else if(section == 1)
    {
        ZPMyInfoSecondTabViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_secondCell];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ZPMyInfoSecondTabViewCell" owner:self options:nil] lastObject];
            _myInfoCollectView = [[UICollectionView alloc] initWithFrame:cell.frame collectionViewLayout:self.myInfoFlowLayout];
            _myInfoCollectView.backgroundColor = clear_color;
            [_myInfoCollectView registerNib:[UINib nibWithNibName:NSStringFromClass([ZPMyinfoCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:reuseIdentifier_collectionViewCell];
            _myInfoCollectView.delegate   = self;
            _myInfoCollectView.dataSource = self;
            _myInfoCollectView.scrollEnabled = NO;
            [cell.contentView addSubview:_myInfoCollectView];
            [_myInfoCollectView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(cell.mas_top).with.offset(0);
                make.bottom.equalTo(cell.mas_bottom).with.offset(0);
                make.leading.equalTo(cell.mas_leading).with.offset(0);
                make.trailing.equalTo(cell.mas_trailing).with.offset(0);
            }];
            
        }

        return cell;
    }
    else
    {
        ZPMyInfoFirstTabViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_firstCell];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ZPMyInfoFirstTabViewCell" owner:self options:nil] lastObject];
        }
        [cell.myInfoSign setHidden:YES];
        [cell.myInfoScore setHidden:YES];
        [cell.myInfoScoreLab setHidden:YES];
        [cell.myInfoScoreImgV setHidden:YES];
        cell.backgroundColor = clear_color;
        [cell.hotlineBtn addTarget:self action:@selector(phoneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
}

#pragma mark - <拨打客服热线>
- (void)phoneBtnClick:(id)sender
{
//    _callAlertView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZPCallAlertView class]) owner:self options:nil] lastObject];
//    [self.callAlertView setStatus];
//    _callAlertView.frame = CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height);
//    __weak ZPMineViewController *weakSelf = self;
//    _callAlertView.cancel = ^(){ // 点击取消
//        [UIView animateWithDuration:0.3 animations:^{
//            weakSelf.callAlertView.concentView.xj_bottom = 1000.;
//        } completion:^(BOOL finished) {
//            [weakSelf.callAlertView removeFromSuperview];
//        }];
//    };
//    
//    _callAlertView.call = ^(){ // 点击拨打电话
//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://02558115681"]];
//    };
//    
//    [self.view.window addSubview:self.callAlertView];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://02558115681"]];
}

- (void)alertController
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"拨打务热线025-58115681?" preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按钮
    __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://02558115681"]];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 44.;
    } else if (indexPath.section == 1) {
        return 200.;
    } return  44.;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 122.;
    } else  {
        return 6.;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        return 10.;
    }
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginBtnClick:)];
        
        [self.headerView.myInfoPortrait addGestureRecognizer:tap];
        tap.numberOfTapsRequired = 1;
        [self.headerView.loginRegBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
        return self.headerView;
    }
    else {
        return nil;
    }
}


#pragma mark - <点击我的积分>
- (void)myScoreBtnClick
{
    if (!self.isLogin) {// 判断是否登录
        [self login];
        return;
    }
    ZPMineScoreViewController *mineScoreVc = [[ZPMineScoreViewController alloc] init];
    mineScoreVc.hidesBottomBarWhenPushed = YES;
    mineScoreVc.score = [NSString stringWithFormat:@"%d",score];
    [self.navigationController pushViewController:mineScoreVc animated:YES];
}



#pragma mark - <点击签到>
- (void)myInfoSignClick
{
    if (!self.isLogin) {// 判断是否登录
        [self login];
        return;
    }
    if(![signStr isEqualToString:@"已签到"]){
        [self signAction];
    }
}



#pragma mark - <展示签到成功图片>
- (void)showSignView
{
    if (!signView) {
        signView = [[UIView alloc] init];
        signView.bounds = CGRectMake(0, 0, 110, 70);
        signView.center = CGPointMake(CENTER_X(self.myInfoTableView), CENTER_Y(self.myInfoTableView) - 115);
        signView.backgroundColor = zFreeColor(0, 0, 0, 0.65);
        ViewRadius(signView, 4.);
        [self.myInfoTableView  addSubview:signView];
        
        UIImageView  *imageView = [[UIImageView  alloc] initWithFrame:CGRectMake(45, 10, 20, 22)];
        imageView.image = Z_IMAGE(@"coin");
        [signView  addSubview:imageView];
        
        UILabel  *label = [[UILabel  alloc] initWithFrame:CGRectMake(15, 40, 80, 20)];
        [[ZPShareView sharedSingleView] setupLabel:label masksToBounds:NO cornerRadius:0 backgroundColor:clear_color text:[NSString  stringWithFormat:@"积分 +%@",scoreStr] textColor:white_color font: 14 textAlignment:NSTextAlignmentCenter];
        
        [signView  addSubview:label];
    }
    
    signView.hidden = NO;
}



- (void)hiddenSignView
{
    if (signView) {
        signView.hidden = YES;
    }
}



#pragma mark - <UICollectionViewDataSource>
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { // 分组数
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { //单元个数
    return self.mineData.myInfoImages.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // 单元内容
    ZPMyinfoCollectionViewCell * item = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier_collectionViewCell forIndexPath:indexPath];
    if (item == nil) {
        item = [[ZPMyinfoCollectionViewCell alloc]init];
    }
    item.myInfoImageView.image = [UIImage imageNamed:self.mineData.myInfoImages[indexPath.row]];
    item.myInfoTitltLael.text = self.mineData.myInfoTitles[indexPath.row];
    return item;
}


#pragma mark - <UICollectionViewDelegate>
// 选中item
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        NSInteger item = [indexPath row];
        if (item == 6) {// 帮助中心
            UIViewController *viewController = [[ZPMineHelpCenterViewController alloc] init];
            
            [self.navigationController pushViewController:viewController animated:YES];
            return;
        }
        
        if (!self.isLogin) {// 判断是否登录状态
            [self login];
            return;
        }
        
        switch (item) {
            case 0: // 我的资料
            {
                if (!self.isLogin) {
                    [self login];
                }
                UIViewController *viewController = [[ZPMineInformationVC alloc] init];
                
                [self.navigationController pushViewController:viewController animated:YES];
            } break;
                
            case 1: // 我的订单
            {
                if (!self.isLogin) {
                    [self login];
                }
                UIViewController *viewController = [[ZPMineOrderViewController alloc] init];
                
                [self.navigationController pushViewController:viewController animated:YES];
            } break;
                
            case 2: //
            {
                if (!self.isLogin) {
                    [self login];
                }
                UIViewController *viewController = [[CourseListViewController alloc] init];
                
                [self.navigationController pushViewController:viewController animated:YES];
            } break;
                
            case 3: //我的班级
            {
                if (!self.isLogin) {
                    [self login];
                    return;
                }
                UIViewController *viewController = [[ZPMineClassViewController alloc] init];
                
                [self.navigationController pushViewController:viewController animated:YES];
            }  break;
                
            case 4: // 切换学生
            {
                if (!self.isLogin) {
                    [self login];
                }
                
                UIViewController *viewController = [[ZPMineChangeStuViewController alloc] init];
                
                [self.navigationController pushViewController:viewController animated:YES];
            } break;
            case 5: // 二维码
            {
                if (!self.isLogin) {
                    [self login];
                }
                
                UIViewController *viewController = [[ZPMineQRCodeViewController alloc]init];
                [self.navigationController pushViewController:viewController animated:YES];
            } break;
            case 7: // 意见反馈
            {
                UIViewController *viewController = [[ZPMineFeedbackViewController alloc] init];
                
                [self.navigationController pushViewController:viewController animated:YES];
            } break;
        }
    

}


// 返回collectionView里的某个section的item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    CGFloat myInfoView_W = self.myInfoView.frame.size.width;
    //    CGFloat myInfoView_H = self.myInfoView.frame.size.height;
    return CGSizeMake((self.myInfoCollectView.xj_width -0.99) / 4, self.myInfoCollectView.xj_height / 2);
}

// 返回collectionView里的某个组里item最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.5;
}

//  返回collectionView里的某个组里item最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.3;
}


#pragma mark - <设置用户头像>
- (void)loadUserHeadImage
{
    if ([[ZPAuthStore sharedZPAuthStore].userToDic objectForKey:@"headUrl"]) {
        [self.headerView.myInfoPortrait sd_setImageWithURL:[NSURL URLWithString:[[ZPAuthStore sharedZPAuthStore].userToDic objectForKey:@"headUrl"]] placeholderImage:[UIImage imageNamed:@"headportrait"] options:SDWebImageRetryFailed];
    }
    self.headerView.myInfoName.text = _stuName;
}



#pragma mark - <根据是否登录刷新界面>
- (void)checkLogIn
{
    //已登录过用户资料
    NSMutableDictionary *dataDic = [[ZPAuthStore sharedZPAuthStore] userToDic];
    NSString *token = [dataDic valueForKey:@"token"];
    NSString *memberId = [dataDic valueForKey:@"memberId"];
    if ([token length] <=0 ||[token isEqual:[NSNull null]] || [memberId length] <=0 ||[memberId isEqual:[NSNull null]]) {
        // 未登录子视图控件的显现状态
        [self unLoginViewStatus];
    } else {
        
        if ([[ZPAuthEngine sharedZPAuthEngine] isValid]) {
            self.isLogin = YES;
            self.headerView.loginRegBtn.hidden = YES;
            self.headerView.loginRegLab.hidden = YES;
            self.headerView.myInfoName.hidden  = NO;
            _memberId = [dataDic valueForKey:@"memberId"];
            _atoken = [dataDic valueForKey:@"token"];
            score = [[dataDic valueForKey:@"score"]intValue];
            _stuName= [dataDic valueForKey:@"stuName"];
            // [self initMyEducation]; // 初始化
            // [self getNumber];  // 获取订单个数
            [self loadUserHeadImage]; // 设置头像
            [self requestSignData];
        } else {
            [self unLoginViewStatus];
        }
    }
}


#pragma mark - <设置未登录状态>
- (void)unLoginViewStatus
{
    self.isLogin = NO;
    self.headerView.loginRegBtn.hidden = NO; // 登录按钮不隐藏
    self.headerView.loginRegLab.hidden = NO;
    self.headerView.myInfoName.hidden  = YES;// 用户名Lab隐藏
    
    signStr      = @"签到";
    score        = 0;
    self.headerView.myInfoPortrait.image = Z_IMAGE(@"headportrait"); // 系统默认头像
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:Z_IMAGE(@"zhenpin_un_news") style:UIBarButtonItemStylePlain target:self action:@selector(clickNewsBtnItem)]; // 导航展示无消息图片
    [self.myInfoTableView reloadData];
}


#pragma mark - <点击头像>
- (void)loginBtnClick:(UIGestureRecognizer *)tap
{
    if (!self.isLogin) {
        [self login];
    } else {
        [self enterMemberCenter];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter  defaultCenter] removeObserver:self name:@"changeMessageIconStatus" object:nil];
}



@end
